//
// Created by Ctzzz on 2024/4/28.
//

#include "TextureMapSample.h"
#include "GLUtils.h"

TextureMapSample::TextureMapSample() {
    // 初始化纹理 ID, 设置为 GL_NONE 0
    m_TextureId = GL_NONE;
}

TextureMapSample::~TextureMapSample() {
    // 如果纹理 ID 不为 GL_NONE 0，则删除纹理
//    if (m_TextureId != GL_NONE) {
//        glDeleteTextures(1, &m_TextureId);
//        m_TextureId = GL_NONE;
//    }
    NativeImageUtil::FreeNativeImage(&m_RenderImage);
}

void TextureMapSample::Init() {
    // 创建纹理
    CreateTexture();

    char vShaderStr[] =
            "#version 300 es                            \n"
            "layout(location = 0) in vec4 a_position;   \n"
            "layout(location = 1) in vec2 a_texCoord;   \n"
            "out vec2 v_texCoord;                       \n"
            "void main()                                \n"
            "{                                          \n"
            "   gl_Position = a_position;               \n"
            "   v_texCoord = a_texCoord;                \n"
            "}                                          \n";

    char fShaderStr[] =
            "#version 300 es                                     \n"
            "precision mediump float;                            \n"
            "in vec2 v_texCoord;                                 \n"
            "layout(location = 0) out vec4 outColor;             \n"
            "uniform sampler2D s_TextureMap;                     \n"
            "void main()                                         \n"
            "{                                                   \n"
            "  outColor = texture(s_TextureMap, v_texCoord);     \n"
            "}                                                   \n";

    m_ProgramObj = GLUtils::CreateProgram(vShaderStr, fShaderStr, m_VertexShader, m_FragmentShader);

    if (m_ProgramObj) {
        // 获取纹理采样器的位置
        m_SamplerLoc = glGetUniformLocation(m_ProgramObj, "s_TextureMap");
    } else {
        LOGCATE("TextureMapSample::Init create program fail");
    }
}

void TextureMapSample::Draw() {
    LOGCATE("TextureMapSample::Draw");
    // 如果纹理 ID 为 GL_NONE 0或者program为0，则返回
    if (m_ProgramObj == GL_NONE || m_TextureId == GL_NONE) return;
    // 激活纹理单元，这里激活的是 GL_TEXTURE0
    glActiveTexture(GL_TEXTURE0);
    // 绑定纹理，将纹理 ID 绑定到 GL_TEXTURE_2D 上，后续的操作都是基于这个纹理 ID
    glBindTexture(GL_TEXTURE_2D, m_TextureId);
    // 更新纹理采样器的值，这里设置为 0，表示使用 GL_TEXTURE0，如果有多个纹理，可以设置为 1，2，3...
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_RenderImage.width, m_RenderImage.height, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, m_RenderImage.ppPlane[0]);
    // 绑定纹理，设置成GL_NONE, 释放纹理
    glBindTexture(GL_TEXTURE_2D, GL_NONE);

    GLfloat verticesCoords[] = {
            -1.0f, 0.42f, 0.0f,  // Position 0
            -1.0f, -0.42f, 0.0f,  // Position 1
            1.0f, -0.42f, 0.0f,  // Position 2
            1.0f, 0.42f, 0.0f,  // Position 3
    };

    GLfloat textureCoords[] = {
            0.0f, 0.0f,        // TexCoord 0
            0.0f, 1.0f,        // TexCoord 1
            1.0f, 1.0f,        // TexCoord 2
            1.0f, 0.0f         // TexCoord 3
    };

    GLushort indices[] = {0, 1, 2, 0, 2, 3};

    // 使用程序
    glUseProgram(m_ProgramObj);
    // 设置顶点坐标
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), verticesCoords);
    // 加载纹理坐标
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), textureCoords);
    // 启用顶点坐标
    glEnableVertexAttribArray(0);
    // 启用纹理坐标
    glEnableVertexAttribArray(1);
    // 绑定RGBA map
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_TextureId);
    //将 RGBA 贴图采样器设置为纹理单位设置为 0
    glUniform1i(m_SamplerLoc, 0);
    // 绘制
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);

}

void TextureMapSample::LoadImage(NativeImage *pImage) {
    LOGCATE("TextureMapSample::LoadImage pImage = %p", pImage);
    if (pImage) {
        m_RenderImage.width = pImage->width;
        m_RenderImage.height = pImage->height;
        m_RenderImage.format = pImage->format;
        NativeImageUtil::CopyNativeImage(pImage, &m_RenderImage);
    }
}

void TextureMapSample::CreateTexture() {
    // 激活纹理单元，这里激活的是 GL_TEXTURE0
    glActiveTexture(GL_TEXTURE0);
    // 生成纹理
    glGenTextures(1, &m_TextureId);
    // 绑定纹理，将纹理 ID 绑定到 GL_TEXTURE_2D 上，后续的操作都是基于这个纹理 ID
    glBindTexture(GL_TEXTURE_2D, m_TextureId);
    // 设置纹理环绕方式，GL_TEXTURE_WRAP_S 表示 X 轴方向，GL_TEXTURE_WRAP_T 表示 Y 轴方向
    // 设置为 GL_CLAMP_TO_EDGE，表示超出纹理坐标范围的像素，使用边缘颜色填充
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    // 设置纹理参数 , 设置为 GL_LINEAR 线性过滤
    // GL_TEXTURE_MIN_FILTER 表示缩小过滤器，GL_TEXTURE_MAG_FILTER 表示放大过滤器
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

void TextureMapSample::DeleteTexture() {
    if(m_TextureId){
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, GL_NONE);
        glDeleteTextures(1, &m_TextureId);
        m_TextureId = GL_NONE;
    }
}
